import { Toast, Dialog } from 'vant'
import { CART_LIST, CHECK_ALL, UN_CHECK_ALL, CHECK_ITEM } from './mutations-types'
import router from '@/router'
//引入封装后的axios
import http from "@/common/api/request"
export default {
    state: {
        list: [],   //购物车的数据
        selectList: []//选中的数据
    },
    getters: {
        isCheckedAll (state) {
            return state.list.length == state.selectList.length
        },
        total (state) {
            let total = {
                num: 0,
                price: 0,

            }
            state.list.forEach(v => {
                if (v.checked) {
                    total.num += v.goods_num
                    total.price += v.goods_price * v.goods_num
                }

            })
            return total
        }
    },
    mutations: {
        [CART_LIST] (state, cartArr) {
            state.list = cartArr
            state.selectList = []
            cartArr.forEach(v => {
                state.selectList.push(v.id)
            })
        },
        //全选
        [CHECK_ALL] (state) {
            state.selectList = state.list.map(v => {
                v.checked = true
                return v.id
            })
        },
        //全不选
        [UN_CHECK_ALL] (state) {
            state.list.forEach(v => {
                v.checked = false
            })
            state.selectList = []
        },
        //单选
        [CHECK_ITEM] (state, index) {
            let id = state.list[index].id
            let i = state.selectList.indexOf(id)
            //能在selectList找到对应的id,就删除
            if (i > -1) {
                return state.selectList.splice(i, 1)
            }
            //如果之前没有选中，就给selectList添加一个id进去
            state.selectList.push(id)

        }
    },
    actions: {
        checkAllFn ({ commit, getters }) {
            getters.isCheckedAll ? commit('unCheckAll') : commit('checkAll')
        },
        //删除购物车产品
        delGoodsFn ({ state }, id) {
            if (state.selectList.length == 0) {
                Toast('请选择商品')
                return
            }
            let arrCart = []
            Dialog.confirm({

                message: '确定删除所选商品？',
            })
                .then(() => {
                    if (typeof id == 'number') {
                        //单个删除
                        arrCart = [id]
                    } else {
                        //多选删除
                        arrCart = state.selectList
                    }
                    http.$axios({
                        url: '/api/deleteCart',
                        method: 'post',
                        data: {
                            ids: arrCart,
                        }
                    }).then((res) => {
                        if (res.success) {
                            Toast(res.msg)
                            router.go(0)

                        }
                    })
                })
                .catch(() => {
                    return
                })




        }
    }
}